package enumandlambda;

/**
 * @author shkstart
 * 对比用户线程和守护线程的区别
 * @create 2022-11-30-20:00
 */
public class ThreadDemoByDaemon2 {
    public static void main(String[] args) {
       // userThread();
        daemonThread();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //收回线程
    private static void daemonThread() {
        Thread t = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println("执行："+ i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //设置守护线程
        t.setDaemon(true);
        t.start();
    }

    private static void userThread() {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 10; i++) {
                System.out.println("执行："+ i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }
}
